using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BasePlayer : MonoBehaviour, IPlayer
{
	protected BallType m_TargetBallType;

	protected List<PoolBall> m_TargetBalls = new List<PoolBall>();

	private int m_Combo;

	[SerializeField]
	protected CountdownOutline m_Outline;

	[SerializeField]
	protected Image m_Avatar;

	[SerializeField]
	public Text playerName;

	[SerializeField]
	protected GridLayoutGroup m_SlotsRoot;

	public virtual BallType TargetBallType
	{
		get
		{
			return m_TargetBallType;
		}
		set
		{
			m_TargetBallType = value;
		}
	}

	public List<PoolBall> BallsList => m_TargetBalls;

	public int combo
	{
		get
		{
			return m_Combo;
		}
		set
		{
			m_Combo = value;
			GameStatistics.MarkMaxCombo(m_Combo);
		}
	}

	public int playerID
	{
		get;
		set;
	}

	protected virtual void Awake()
	{
		m_TargetBallType = BallType.NONE;
		playerID = GetInstanceID();
		playerName.text = base.name;
		m_Outline.enabled = false;
	}

	protected void AddBalls(int min, int max)
	{
		for (int i = min; i <= max; i++)
		{
			if (!Pools.Balls[i].IsBallDisable())
			{
				m_TargetBalls.Add(Pools.Balls[i]);
				Image image = SupportTools.AddChild<Image>(m_SlotsRoot.gameObject, "UI/BattleScene/Slot");
				image.sprite = Resources.Load<Sprite>("BallsIcon/" + i.ToString());
				image.transform.name = i.ToString();
			}
		}
	}

	public void UpdateBallIcon()
	{
		if (m_TargetBallType == BallType.NONE)
		{
			return;
		}
		if (m_TargetBalls.Count != 0)
		{
			int num = 0;
			while (num < m_TargetBalls.Count)
			{
				if (m_TargetBalls[num].BallState == PoolBall.State.POTTED)
				{
					Transform transform = m_SlotsRoot.transform.Find(m_TargetBalls[num].GetBallID().ToString());
					if ((bool)transform)
					{
						UnityEngine.Object.Destroy(transform.gameObject);
					}
					m_TargetBalls.RemoveAt(num);
				}
				else
				{
					num++;
				}
			}
			if (m_TargetBalls.Count == 0 && m_TargetBallType != BallType.BLACK)
			{
				m_TargetBallType = BallType.BLACK;
			}
		}
		if (m_TargetBalls.Count == 0)
		{
			if (m_TargetBallType == BallType.BLACK)
			{
				AddBalls(8, 8);
			}
			if (m_TargetBallType == BallType.SOLID)
			{
				AddBalls(1, 7);
				if (!(this is AIPlayer))
				{
					BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(405), "' " + gameObject.name + " '"));
				}
			}
			if (m_TargetBallType == BallType.STRIPE)
			{
				AddBalls(9, 15);
				if (!(this is AIPlayer))
				{
					BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(406), "' " + gameObject.name + " '"));
				}
			}
		}
	}

	public void Countdown(float percentage)
	{
		if (m_Outline.enabled)
		{
			m_Outline.SetValue(percentage);
		}
	}

	public virtual void Begin()
	{
	}

	public virtual void End()
	{
	}

	public virtual void PlayerUpdate()
	{
	}
}
